#（仅限windows系统）将正在占用特定端口号的进程结束
param (
    [int]$port
)

if(-not $port){
    Write-Error "请提供要结束进程的占用端口号"
    Exit
}

# 查找占用指定端口的进程
$process = Get-NetTCPConnection 
    | Where-Object { $_.LocalPort -eq $port} 
    | Select-Object -ExpandProperty OwningProcess

if ($process) {
    # 获取进程的详细信息
    $processInfo = Get-Process -Id $process
    
    # 输出进程信息
    Write-Host "找到占用端口 $port 的进程："
    Write-Host "进程ID: $($processInfo.Id)"
    Write-Host "进程名称: $($processInfo.ProcessName)"

    # 确认是否结束进程
    $confirm = Read-Host "是否结束该进程？(Y/N)"
    if ($confirm.ToLower() -eq 'y') {
        Stop-Process -Id $process -Force
        Write-Host "进程已结束"
    } else {
        Write-Host "未结束进程"
    }
} else {
    Write-Host "未找到占用端口 $port 的进程"
}